$(function () {

    let baseURL = "http://localhost:8080/api/v1";
    $(".input_sub").click(function (e) {

        //阻止默认行为
        e.preventDefault();
        let username = $(".input_txt").val().trim();
        let userpwd = $(".input_pass").val().trim();

        if (username === "" || userpwd === "") {
            return alert("用户名或者密码不能为空");
        }

        //http://localhost:8080/api/v1

        $.ajax({
            method: 'POST',
            url: baseURL + '/admin/user/login',
            data: {
                username: username,
                password: userpwd
            },
            // dataType不写的话默认是json
            dataType: 'json',
            success: (res) => {
                console.log(res);
                if (res.code == 200) {
                    window.localStorage.setItem("token", res.token);
                    $('#myModal').find("h4").text(res.code);
                    $('#myModal').find(".modal-body>span").text(res.msg + "点击跳转");
                    //模态框的跳转，按下esc可以退出模态框
                    $('#myModal').modal({
                        keyboard: true
                    });
                    //这是cv过来的固有写法，监听模态框的hide方法触发就触发以下事件
                    $('#myModal').on('hidden.bs.modal', function (e) {
                        //location.href = "跳转页面"   是=  不是 ()
                        window.location.href = "index.html";
                    })

                }
                else {
                    $('#myModal').find("h4").text("错误类型：" + res.code);
                    $('#myModal').find(".modal-body>span").text(res.msg);
                    $('#myModal').modal({
                        keyboard: true
                    });
                    console.log(res);
                }
            }
        })
    })
})